package com.sixth.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sixth.core.entity.InventoryLog;
import com.sixth.core.vo.R;
import com.sixth.stock.service.InventoryLogService;
import com.sixth.stock.mapper.InventoryLogMapper;
import com.sixth.stock.vo.InventoryLogVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
* @author Administrator
* @description 针对表【stock_inventory_log】的数据库操作Service实现
* @createDate 2024-08-14 19:56:44
*/

@Service
public class InventoryLogServiceImpl extends ServiceImpl<InventoryLogMapper, InventoryLog>
    implements InventoryLogService{
    @Resource
    private InventoryLogMapper inventoryLogMapper;
    @Override
    public R<IPage<InventoryLog>> selectPageAll(InventoryLogVo inventoryLogVo) {
        Integer pageNum = inventoryLogVo.getPageNum();
        Integer pageSize = inventoryLogVo.getPageSize();
        if (pageNum == null || pageSize == null|| pageNum <= 0 || pageSize <= 0) {
            pageNum = 1;
            pageSize = 5;
        }
        IPage<InventoryLog> page = new Page<>(pageNum, pageSize);
        QueryWrapper<InventoryLog> queryWrapper = new QueryWrapper<>();
        if (inventoryLogVo.getPurchaseId() != null&&!inventoryLogVo.getPurchaseId().equals("")) {
            queryWrapper.like("purchase_id", inventoryLogVo.getPurchaseId());
        }
        if (inventoryLogVo.getMedicinesName() != null&&!inventoryLogVo.getMedicinesName().equals("")) {
            queryWrapper.like("medicines_name", inventoryLogVo.getMedicinesName());
        }
        if (inventoryLogVo.getMedicinesType() != null&&!inventoryLogVo.getMedicinesType().equals("")) {
            queryWrapper.eq("medicines_type", inventoryLogVo.getMedicinesType());
        }
        if (inventoryLogVo.getPrescriptionType() != null&&!inventoryLogVo.getPrescriptionType().equals("")) {
            queryWrapper.eq("prescription_type", inventoryLogVo.getPrescriptionType());
        }
        if (inventoryLogVo.getProducterId() != null&&!inventoryLogVo.getProducterId().equals("")) {
            queryWrapper.eq("producter_id", inventoryLogVo.getProducterId());
        }
        if (inventoryLogVo.getDateRange() != null && inventoryLogVo.getDateRange().size() == 2) {
            queryWrapper.between("create_time", inventoryLogVo.getDateRange().get(0), inventoryLogVo.getDateRange().get(1));
        }
        queryWrapper.orderByDesc("create_time");
        IPage<InventoryLog> inventoryLogIPage = inventoryLogMapper.selectPage(page, queryWrapper);
        return R.ok(inventoryLogIPage);
    }
}

